use async_trait::async_trait;
use base_data::channel::{ChannelState, EndReason};
use std::fmt::Debug;
use tools::ResultNull;

use super::{inbound400::context::InboundRunContext, RunContext};

#[async_trait]
pub trait Business: Send + Sync + 'static + Debug {
	type C: RunContext;

	///当此操作为最后一个操作时，是否进行上传结束消息.
	/// 目前仅无此业务号码操作，因为没有来电记录，所以不上传结束消息.
	fn need_upload_close_msg(&self) -> bool {
		true
	}

	///当一个彩铃播放完成后的操作
	async fn play_back_stop(&self, _context: &mut Self::C, _channel_id: u32) -> ResultNull {
		Ok(())
	}

	///返回下一个操作的节点.有可能返回自己当前节点.
	fn next_bus(&'static mut self, context: &mut Self::C) -> &'static dyn Business<C = Self::C>
	where
		Self: Sized,
	{
		match context.take_next_handler() {
			Some(h) => h,
			None => self,
		}
	}

	async fn bridge(&self, _context: &mut Self::C) -> ResultNull {
		Ok(())
	}

	///挂断的业务逻辑
	async fn hangup(
		&self,
		_context: &mut Self::C,
		_channel_id: u32,
		_reason: EndReason,
	) -> ResultNull {
		Ok(())
	}

	///有通道应答的业务逻辑
	async fn answer(&self, _context: &mut Self::C, _channel_id: u32) -> ResultNull {
		Ok(())
	}

	///开始执行呼叫的业务.
	async fn start_call(&self, context: &mut Self::C) -> ResultNull;

	///结束所有呼叫过程
	async fn kill_all(&self, _context: &mut Self::C) -> ResultNull {
		Ok(())
	}

	///结束的操作,用来计费,上传等等.在通道退出前调用
	async fn closeing(&self, _context: &mut Self::C) -> ResultNull {
		Ok(())
	}
	///执行过程当中定时执行调用的检查.可以进行费用\时长等的检查.
	async fn check(&self, _context: &mut Self::C) -> ResultNull {
		Ok(())
	}
	///得到按键的操作.
	async fn get_dtmf(&self, _context: &mut Self::C,_id:u32, _dtmf: char) -> ResultNull {
		Ok(())
	}
	async fn change_call_state(
		&self,
		_context: &mut InboundRunContext,
		_id: u32,
		_state: ChannelState,
	) -> ResultNull {
		Ok(())
	}
}

#[derive(Debug)]
pub(crate) struct NoHandler;

#[async_trait]
impl Business for NoHandler {
	type C = InboundRunContext;

	async fn start_call(&self, _context: &mut Self::C) -> ResultNull {
		Ok(())
	}
}
